package com.lyu.xiaolvshu.kv.biz.service.impl;

import com.lyu.framework.common.exception.BizException;
import com.lyu.framework.common.response.Response;
import com.lyu.xiaolvshu.kv.biz.domain.dos.NoteContentDO;
import com.lyu.xiaolvshu.kv.biz.domain.repository.NoteContentRepository;
import com.lyu.xiaolvshu.kv.biz.enums.ResponseCodeEnum;
import com.lyu.xiaolvshu.kv.biz.service.NoteContentService;
import com.lyu.xiaolvshu.kv.dto.req.AddNoteContentReqDTO;
import com.lyu.xiaolvshu.kv.dto.req.DeleteNoteContentReqDTO;
import com.lyu.xiaolvshu.kv.dto.req.FindNoteContentReqDTO;
import com.lyu.xiaolvshu.kv.dto.resp.FindNoteContentRespDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

@Service
@Slf4j
public class NoteContentServiceImpl implements NoteContentService {

    @Resource
    private NoteContentRepository noteContentRepository;

    @Override
    public Response<?> addNoteContent(AddNoteContentReqDTO addNoteContentReqDTO) {
        String uuid = addNoteContentReqDTO.getUuid();
        String content = addNoteContentReqDTO.getContent();

        NoteContentDO noteContentDO = NoteContentDO.builder()
                .id(UUID.fromString(uuid))
                .content(content)
                .build();

        noteContentRepository.save(noteContentDO);

        return Response.success();
    }

    @Override
    public Response<FindNoteContentRespDTO> findNoteContent(FindNoteContentReqDTO findNoteContentReqDTO) {
        String uuid = findNoteContentReqDTO.getUuid();
        Optional<NoteContentDO> optional = noteContentRepository.findById(UUID.fromString(uuid));
        if (optional.isEmpty()) {
            throw new BizException(ResponseCodeEnum.NOTE_CONTENT_NOT_FOUND);
        }
        FindNoteContentRespDTO findNoteContentRespDTO = FindNoteContentRespDTO.builder()
                .uuid(optional.get().getId())
                .content(optional.get().getContent())
                .build();
        return Response.success(findNoteContentRespDTO);
    }

    @Override
    public Response<?> deleteNoteContent(DeleteNoteContentReqDTO deleteNoteContentReqDTO) {
        String uuid = deleteNoteContentReqDTO.getUuid();
        noteContentRepository.deleteById(UUID.fromString(uuid));
        return Response.success();
    }
}
